const { createProxyMiddleware } = require('http-proxy-middleware')

module.exports = function (app) {
  app.use(
    //遇见/api1前缀的请求,就会触发该代理配置
    createProxyMiddleware('/api1', {
      target: 'http://localhost:5000', //请求转发给谁

      //控制服务器收到的请求头中Host字段的值,
      //不加localhost:3000,加了localhost:5000
      changeOrigin: true,

      //重写请求路径
      //加上下面这行代码,请求路径由 /api/students -----------> students
      pathRewrite: { '^/api1': '' }
    }),
    createProxyMiddleware('/api2', {
      target: 'http://localhost:5001',
      changeOrigin: true,
      pathRewrite: { '^/api2': '' }
    })
  )
}
